﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.ThrongIn.BE;
using Com.ThrongIn.BLL;
using Com.ThrongIn.Utilities;
namespace ThrongIn
{
    public partial class ProfileList : System.Web.UI.UserControl
    {
#region Properties
        public int Columns
        {
            get
            {
                if(ViewState["RepeatColumns"] == null)
                    return 4;
                else
                    return (int)ViewState["RepeatColumns"];
            }
            set
            {
                ViewState["RepeatColumns"] = value;
            }
        }
        public RepeatLayout Layout
        {
            get
            {
                if (ViewState["Layout"] == null)
                    return RepeatLayout.Table;
                else
                    return (RepeatLayout)ViewState["Layout"];
            }
            set
            {
                ViewState["Layout"] = value;
            }
        }
        public RepeatDirection Direction
        {
            get
            {
                if (ViewState["RepeatDirection"] == null)
                    return RepeatDirection.Horizontal;
                else
                    return (RepeatDirection)ViewState["RepeatDirection"];
            }
            set
            {
                ViewState["RepeatDirection"] = value;
            }
        }
        public List<Profile> DataSource
        {
            get;
            set;
        }
#endregion
        
        protected void profileDataList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                System.Web.UI.HtmlControls.HtmlImage img;
                if (Direction == RepeatDirection.Vertical)
                {
                    img = e.Item.FindControl("imgProfile1") as System.Web.UI.HtmlControls.HtmlImage;
                     System.Web.UI.HtmlControls.HtmlGenericControl boxSmall = e.Item.FindControl("divBoxSmall") as  System.Web.UI.HtmlControls.HtmlGenericControl;
                    boxSmall.Visible = true;
                }
                else
                {
                    img = e.Item.FindControl("imgProfile") as System.Web.UI.HtmlControls.HtmlImage;
                     System.Web.UI.HtmlControls.HtmlGenericControl box = e.Item.FindControl("divBox") as  System.Web.UI.HtmlControls.HtmlGenericControl;                    
                    box.Visible = true;
                }
                if (img != null)
                {
                    byte[] binaryImg = (DataBinder.Eval(e.Item.DataItem, "Profile_Image")) as byte[];
                    if (binaryImg == null)
                    {
                        img.Src = ResolveUrl(String.Format("~/Handler/ImageHandler.ashx?image={0}&width=120&height=120", "Images/No-Image-Available.gif"));
                    }
                    else
                    {
                        byte[] afterResize =  ImageResizer.ByteFromBitmap(ImageResizer.ResizeImage(ImageResizer.BitmapFromByteArray(binaryImg), 120, 120, ImageResizer.ResizeOptions.MaxWidthAndHeight));
                       
                        img.Src = "data:image/jpg;base64," + Convert.ToBase64String(afterResize);                        
                    }
                }
                
            }
        }

        #region User Define Methods
        public void BindControl()
        {
            profileDataList.RepeatColumns = Columns;
            profileDataList.RepeatDirection = Direction;
            profileDataList.RepeatLayout = Layout;
            profileDataList.DataSource = DataSource;
            profileDataList.DataBind();
        }
#endregion

    }
}